﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Threading;
using System.Diagnostics;
using System.Xml;

namespace PachubeDataAccess.Models
{
    public class Favorites
    {
        public static ObservableCollection<Feed> LoadFromIsolatedStorage()
        {
            ObservableCollection<Feed> results = null;

            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("/favorites.xml", System.IO.FileMode.OpenOrCreate, isf))
            {
                using (StreamReader r = new StreamReader(isfs))
                {
                    try
                    {
                        string xml = r.ReadToEnd();
                        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
                        results = new ObservableCollection<Feed>(from element in XElement.Parse(xml).Descendants("feed")
                                                                      select new Feed(element.Element("apikey").Value)
                                                                      {
                                                                          Id = int.Parse(element.Attribute("id").Value),
                                                                          Url = element.Attribute("url").Value,
                                                                          Title = element.Element("title").Value
                                                                      });
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Favorites.cs: Load(): " + ex.Message);
                    }
                }
            }

            return results;
        }

        public static void SaveToIsolatedStorage(ObservableCollection<Feed> feeds)
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("/favorites.xml", System.IO.FileMode.Create, isf))
            {
                XElement root = new XElement("favorites");
                foreach (var feed in feeds)
                {
                    if (feed != null)
                        root.Add(new XElement("feed", 
                            new XAttribute("url", feed.Url), 
                            new XAttribute("id", feed.Id),
                            new XElement("apikey", feed.ApiKey),
                            new XElement("title", feed.Title)));
                }
                using (XmlWriter w = XmlWriter.Create(isfs))
                {
                    root.WriteTo(w);
                    w.Flush();
                }
            }
        }

        public static void Add(Feed f)
        {
            ObservableCollection<Feed> favs = LoadFromIsolatedStorage();
            if (favs == null)
                favs = new ObservableCollection<Feed>();
            favs.Add(f);
            SaveToIsolatedStorage(favs);
        }
    }
}
